<?php
namespace App\GraphQL\Mutations\Ad;

use App\Models\Ad;
use App\Models\User;
use GraphQL\Type\Definition\ResolveInfo;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
use Illuminate\Support\Carbon;

class Create
{

    /**
     * Return a value for the field.
     *
     * @param null $rootValue
     *            Usually contains the result returned from the parent field. In this case, it is always `null`.
     * @param mixed[] $args
     *            The arguments that were passed into the field.
     * @param \Nuwave\Lighthouse\Support\Contracts\GraphQLContext $context
     *            Arbitrary data that is shared between all fields of a single query.
     * @param \GraphQL\Type\Definition\ResolveInfo $resolveInfo
     *            Information about the query itself, such as the execution state, the field name, path to the field from the root, and more.
     * @return mixed
     */
    public function __invoke($rootValue, array $args, GraphQLContext $context, ResolveInfo $resolveInfo)
    {
        $data = $args['input'];
        $authUser = auth()->user();
        $data['user_id']=$authUser->id;
        $data['agent_user_id'] = $data['cate'] === 'NORMAL'?0: $data['agent_user_id'];
        $this->validate($data);
        // 创建广告
        $data['expired_at'] = (new Carbon($data['expired_at']))->format('Y-m-d 23:59:59');
        Ad::create($data);
        return [
            'status' => '200',
            'message' => '创建成功'
        ];
    }

    public function validate($data)
    {
        $cates = [
            'NORMAL',
            'PRIVATE'
        ];
        $rules = [
            'name' => [
                'required',
                'max:18'
            ],
            'cate' => [
                'required',
                Rule::in($cates)
            ],
            'agent_user_id'=>[
                'nullable',
                'numeric'
            ],
            'redirect_url' => [
                'nullable',
                'url'
            ],
            'cover_url' => [
                'required',
                'url'
            ],
            'price'=>[
                'nullable',
                'numeric'
            ],
            'expired_at'=>[
                'required',
                'date'
            ]
        ];
        $messages = [
            'name.required' => '名称不能为空',
            'name.max' => '名称不能超过18个字',
            'cate.required' => '分类不能为空',
            'cover_url.required' => '封面不能为空',
            'cover_url.url' => '封面为url地址',
            'cate.in' => '分类不能超出限制',
            'agent_user_id.numeric' => '代理ID为数值',
            'price.numeric' => '价格为数值',
            'expired_at.required'=>'过期时间不能为空',
            'expired_at.date'=>'过期时间为日期',
            'redirect_url.url' => '跳转地址为url',
        ];
        $validate = Validator::make($data, $rules, $messages);
        if ($validate->fails()) {
            throw new \InvalidArgumentException($validate->messages()->first());
        }
        // 代理
        if($data['agent_user_id']){
            $agent = User::find($data['agent_user_id']);
            if(!$agent){
                throw new \Exception('代理用户不存在');
            }
        }
        return true;
    }
}
